<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/04/0004
 * Time: 14:39
 */

namespace app\sports_admin\controller;

use app\exception\ParentsException;
use app\exception\StudentException;
use think\Request;
use app\sports_admin\model\Parents as ParentsModel;
use app\sports_admin\model\Student as StudentModel;
use app\sports_admin\service\Parents as ParentsService;
use app\sports_admin\model\ParentsChildren;
use app\sports_admin\vaildate\Parents as ParentsVaildate;
class Parents extends Base
{
    protected $beforeActionList = [
        'isUsers'=>['only'=>'index,parentslist,add,parentsinsert,parentsdelete,childrens,childrenlist,childrensdelete,
                    childrensupdatepage,childrensaddpage,childrensadd'],
        'operationSelect'=>['only'=>'index,parentslist,add,childrens,childrenlist,childrensaddpage']
    ];
    //家长信息首页
    public function index()
    {
        return view('index');
    }
    /**
     * 获取家长信息列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public function parentsList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $result = ParentsModel::getParentsList($page,$rows,$sort,$sortOrder,$searchName);
        return $result;
    }
    /**
     * 添加家长页面
    */
    public function add()
    {
        return view('add');
    }
    /**
     * 添加家长信息
    */
    public function parentsInsert(Request $request)
    {
        if($request->isAjax()){
            (new ParentsVaildate())->insert();
            return ParentsService::parentsInsert();
        }
    }

    /**
     * 删除家长信息
     * @param $id 家长信息Id
    */
    public function parentsDelete(Request $request)
    {
        if($request->isAjax()){
            (new ParentsVaildate())->go_check();
            $id = intval($request->post('id'));
            $bool = ParentsModel::destroy($id);
            if(!$bool){
                throw new ParentsException([
                    'msg'=>'删除失败',
                    'errorCode'=>203
                ]);
            }
            return json(['code'=>202,'msg'=>'删除成功']);
        }
    }
    /**
     * 重置密码
     */
    public function passwordReset()
    {
        (new ParentsVaildate())->passwordReset();
        return ParentsService::passwordReset();
    }
    /**
     * 家长子女页面
     * @param $parentId 家长id
     */
    public function childrens()
    {
        $parentId = intval(input('get.parents_id'));
        return view('children',compact('parentId'));
    }
    /**
     * 获取家长子女信息列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public function childrenList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','parentId']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $parentId = intval($params['parentId']);
        $result = ParentsChildren::getChildrenList($page,$rows,$sort,$sortOrder,$searchName,$parentId);
        return $result;
    }
    /**
     * 家长子女信息删除
     * @param id 家长子女信息id
    */
    public function childrensDelete(Request $request)
    {
        if($request->isAjax()){
            $id = intval($request->post('id'));
            $bool = ParentsChildren::destroy($id);
            if(!$bool){
                throw new StudentException([
                    'msg'=>'删除失败',
                    'errorCode'=>203
                ]);
            }
            return json(['code'=>202,'msg'=>'删除成功']);
        }
    }
    //修改子女信息页面
    public function childrensUpdatePage()
    {
        $id   = Request::instance()->only('id');
        $data = ParentsChildren::where('id',$id['id'])->find();
        return view("children_update",compact("data"));
    }
    //修改子女信息
    public function childrensUpdate(Request $request)
    {
        if($request->isAjax()){
            $data = ParentsChildren::childrensUpdate();
            if(!$data){
                throw new StudentException([
                    'msg'=>'修改失败',
                    'errorCode'=>203
                ]);
            }
            return json(['code'=>202,'msg'=>'修改成功']);
        }
    }
    //添加子女信息页面
    public function childrensAddPage()
    {
        $id = Request::instance()->only('parent_id');
        return view('add_children',compact('id'));
    }
    //添加子女信息
    public static function childrensAdd(Request $request)
    {
        if($request->isAjax()){
            $data = ParentsChildren::childrensAdd();
            if(!$data){
                throw new ParentsException([
                    'msg'=>'修改失败',
                    'errorCode'=>203
                ]);
            }
            return json(['code'=>202,'msg'=>'修改成功']);
        }
    }
}